﻿using LeSoft.Application.Bussiness;
using LeSoft.Util;
using LeSoft.Application.Code;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using LeSoft.Application.Entity;
using LeSoft.Application.Cache;

namespace LeSoft.Application.Web.Areas.PropertyManage.Controllers
{
    public class CS_EnergyMeterSettingController : MvcControllerBase
    {

        [HttpGet]
        [HandlerAuthorize(PermissionMode.Enforce)]
        public ActionResult SettingIndex()
        {
            return View();
        }
        [HttpGet]
        [HandlerAuthorize(PermissionMode.Enforce)]
        public ActionResult UnitForm()
        {
            return View();
        }
        [HttpGet]
        [HandlerAuthorize(PermissionMode.Enforce)]
        public ActionResult UnitBatchForm()
        {
            return View();
        }



        public ActionResult GetInstanceList(string queryJson)
        {
            var queryParam = queryJson.ToJObject();
            var data = new EnergyBLL().GetInstanceList();
            if (!queryParam["Nature"].IsEmpty())
            {
                data = data.Where(t => t.Nature == queryParam["Nature"].ToString());
            }
            if (!queryParam["Type"].IsEmpty())
            {
                data = data.Where(t => t.Type == queryParam["Type"].ToString());
            }
            if (!queryParam["PStructId"].IsEmpty())
            {
                var PStructId = queryParam["PStructId"].ToString();
                data = data.Where(t => PStructId.Contains(t.PStructId));
            }
            if (!queryParam["condition"].IsEmpty() && !queryParam["keyword"].IsEmpty())
            {
                var condition = queryParam["condition"].ToString();
                var keyword = queryParam["keyword"].ToString();
                switch (condition)
                {
                    case "Name":
                        data = data.Where(t => t.Name.Contains(keyword));
                        break;
                    case "Code":
                        data = data.Where(t => t.Code.Contains(keyword));
                        break;
                    default:
                        break;
                }
            }
            var pstructData = new PStructCache().GetList();
            foreach (var a in data)
            {
                a.PStructId = pstructData.Count(t => t.Id == a.PStructId) > 0 ? pstructData.FirstOrDefault(t => t.Id == a.PStructId).AllName : "";

                if (!a.ParentId.IsEmpty() && a.ParentId != "0")
                {
                    var parent = new EnergyBLL().GetInstanceEntity(a.ParentId);
                    a.ParentId = parent == null ? "" : parent.Name + "/" + parent.Code + "/" + parent.PSMemo;
                }
            }
            return Content(data.ToJson());
        }


        /// <summary>
        /// 获取费表资料树
        /// </summary>
        /// <param name="Nature"></param>
        /// <param name="Type"></param>
        /// <returns></returns>
        [HttpGet]
        [HandlerAction(OperationType.GetTree, "费表安装", "GetTreeJson")]
        public ActionResult GetTreeJson(string Nature = "", string Type = "")
        {
            var data = new EnergyBLL().GetList();
            if (!string.IsNullOrEmpty(Nature))
                data = data.Where(t => t.Nature == Nature);
            if (!string.IsNullOrEmpty(Type))
                data = data.Where(t => t.Type == Type);
            var treeList = new List<TreeEntity>();
            foreach (EnergyMeterInfoEntity item in data)
            {
                TreeEntity tree = new TreeEntity();
                tree.id = item.Id;
                tree.text = item.Name;
                tree.value = item.Id;
                tree.parentId = "0";
                tree.isexpand = false;
                tree.complete = true;
                tree.hasChildren = false;
                treeList.Add(tree);
            }
            return Content(treeList.TreeToJson());
        }
        public ActionResult GetUnitFormJson(string keyValue)
        {
            var data = new EnergyBLL().GetInstanceEntity(keyValue);
            return Content(data.ToJson());
        }
        public ActionResult SaveUnitForm(string keyValue, EnergyMeterInstanceEntity entity)
        {
            try
            {
                entity.SourceType = "0";
                new EnergyBLL().SaveInstanceForm(keyValue, entity);
                return Success("操作成功。");
            }
            catch (Exception e)
            {
                return Error(e.Message);
            }
        }

        public ActionResult RemoveUnitForm(string keyValue)
        {
            try
            {
                var data = new EnergyBLL().GetInstanceList().Where(t => t.ParentId == keyValue);
                if (data.Count() > 0)
                {
                    return Error("请先删除子节点");
                }
                else
                {
                    new EnergyBLL().RemoveInstanceForm(keyValue);
                    return Success("操作成功。");
                }
            }
            catch (Exception e)
            {
                return Error(e.Message);
            }
        }
    }
}
